package com.cookizz.waterfalldemo;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;

/**
 * 运行时动态调整尺寸的ImageView
 * @author dugd
 * @time 2015-02-28
 */
public class DynamicSpecImage extends ImageView {

	private double mHeightRatio;
	
	public DynamicSpecImage(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	
	public void setHeightRatio(double ratio) {
		mHeightRatio = ratio;
		requestLayout();
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		if (mHeightRatio > 0.0) {
            // set the image views size
            int width = MeasureSpec.getSize(widthMeasureSpec);
            int height = (int) (width * mHeightRatio);
            setMeasuredDimension(width, height);
        }
        else {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
	}
}
